#include "Functions.hpp"

#define check_result(res)                                         \
    if (res)                                                      \
        plhs[0] = bxCreateCStruct(pyobject_sid, res); \
    else                                                          \
    {                                                             \
        Raise_PyErr();                                            \
    }

namespace sympy
{
    using namespace baltam::py;

    void bxCallSymPyFunctionOneArg(
        const char *funcname,
        int nlhs, bxArray *plhs[],
        int nrhs, const bxArray *prhs[])
    {
        if (nrhs != 1)
            bxReturnOverloadFailure();
        PyObject *x = (PyObject *)bxGetCStruct(pyobject_sid, prhs[0]);
        if (!x)
            bxReturnOverloadFailure();
        PyObject *func = pyobject_get_attr_string(sp, funcname);
        PyObject *args[2];
        args[1] = x;
        args[0] = pyobject_vectorcall_pack(
            func, args + 1, 1, 0);
        py_dec_ref(func);
        check_result(args[0]);
    }
}